const path = require('path');

var webpack             = require('webpack');
var ExtractTextPlugin   = require('extract-text-webpack-plugin');
var HtmlWebpackPlugin   = require('html-webpack-plugin');

// less打包
let extractLESS = new ExtractTextPlugin('css/[name].css');


// 环境变量配置，dev / online
var WEBPACK_ENV         = process.env.WEBPACK_ENV || 'dev';

// 获取html-webpack-plugin参数的方法 
var getHtmlConfig = function(name){
    return {
        template    : './src/view/' + name + '.html',
        filename    : 'view/' + name + '.html',
        favicon     : './favicon.ico',
        inject      : true,
        hash        : true,
        chunks      : ['common', name]
    };
 };
 var getHtml404Config = function(name){
    return {
        template    : './src/view/' + name + '.html',
        filename    : 'view/' + name + '.html',
        favicon     : './favicon.ico',
        inject      : true,
        hash        : true,
        chunks      : [name]
    };
 };

var config = {
	// webpack-dev-server的配置
    devServer: {
        proxy: {
            '**/*.do': {
                // target: 'http://www.westonwh.com',
                target: 'http://39.108.9.21',
                changeOrigin: true,
                secure: false
            }
        }
     },
	entry : {
		'common' : ['./src/page/common/index.js'],
		'index' : ['./src/page/index/index.js'],
		'login' : ['./src/page/login/index.js'],
		'category' : ['./src/page/category/index.js'],
		'book' : ['./src/page/book/index.js'],
		'cart' : ['./src/page/cart/index.js'],
		'personalCenter' : ['./src/page/personalCenter/index.js'],
		'orderDetail' : ['./src/page/orderDetail/index.js'],
	},
	output: {//目标文件
        path: __dirname+'/dist/',
        publicPath:'dev' === WEBPACK_ENV ? '/dist/' : '//s.westonwh.com/',//线上环境资源环境访问地址
        // publicPath:'/dist/',
        filename: 'js/[name].js'
    },
    externals : {//外部依赖的声明
        'jquery' : 'window.jQuery'
    }, 
    module: {//各种文件，各种loader
        loaders: [
            { test: /\.css$/, loader: ExtractTextPlugin.extract("style-loader","css-loader") },
            //处理图片和字体；limit=100&name=resource/[name].[ext] 限制文件大小为100，大于100会以文件的形式显示出来resource/
            { test: /\.(gif|png|jpg|woff|svg|eot|ttf)\??.*$/, loader: 'url-loader?limit=100&name=resource/[name].[ext]' },
            { 
                test: /\.string$/,
                loader: 'html-loader',
                query : {
                    minimize : true,    //做最小化压缩
                    removeAttributeQuotes : false //自动移除引号取消
                }
            },
            //less单独打包
            {test: /\.less$/i, loader: extractLESS.extract(['css','less'])}
        ]
    },
    resolve : {//配置别名
        alias : {
            node_modules    : __dirname + '/node_modules',//__dirname表示根目录
            util            : __dirname + '/src/util',
            page            : __dirname + '/src/page',
            service         : __dirname + '/src/service',
            image           : __dirname + '/src/image'
        }
    },
    plugins: [//插件
        extractLESS,
        // 独立通用模块到js/base.js
        new webpack.optimize.CommonsChunkPlugin({
            name : 'common',
            filename : 'js/base.js'
        }),
        // 把css单独打包到文件里
        new ExtractTextPlugin("css/[name].css"),
        // html模板的处理
        new HtmlWebpackPlugin(getHtmlConfig('index')),
        new HtmlWebpackPlugin(getHtmlConfig('login')),
        new HtmlWebpackPlugin(getHtmlConfig('category')),
        new HtmlWebpackPlugin(getHtmlConfig('book')),
        new HtmlWebpackPlugin(getHtmlConfig('cart')),
        new HtmlWebpackPlugin(getHtmlConfig('personalCenter')),
        new HtmlWebpackPlugin(getHtmlConfig('orderDetail')),
    ]
};

if('dev' === WEBPACK_ENV){//定义webpack-dev-server启动为inline方式
    config.entry.common.push('webpack-dev-server/client?http://localhost:8086/');
}

module.exports = config;